<?php
use entities\IM_Model;
use entities\IM_Model_stats;
use entities\IM_Model_picture;

class IM_C_ManageModels 
	extends BaseController{
	
	public function start()
	{
		fw_util_custom_doLoginCheck();
		/*
		 $o_navi = new ClaycodeNaviMenu(
			$this->o_configuration->getApplicationsArray(), 
			array(
				'pretty_urls'=> true,
				'is_sub_navi'=> false
			)
		);
		$this->registry->template->s_navi = $o_navi->getNaviHtml();
		$this->registry->template->s_mainAppDisplay = $this->getModelsTable();
		$this->registry->template->show('Admin-ManageModels.php');
		*/
		$this->showModels();
	}
	
	public function showModels()
	{
		fw_util_custom_doLoginCheck();
		$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
		$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
		$this->getSubNavi();
		$this->registry->template->s_mainAppDisplay = $this->getModelsTable();
		$this->registry->template->show('Admin-ManageModels.php');
	}
	
	public function addModel(){
		fw_util_custom_doLoginCheck();
		$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
		$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
		$o_addModelForm = new ClaycodeForm('adminAddModel');
		
		$this->registry->template->s_mainAppDisplay = $o_addModelForm->getFormHtml();
		$this->getSubNavi();
		$this->registry->template->show('Admin-ManageModels.php');
	}
	
	//shows the statistics of the model
	public function updateModel(){
		fw_util_custom_doLoginCheck();
		$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
		$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
		$s_encryptedModelId = $_GET['ident'];
		
		$i_modelId = EncryptionHandler::get()->doDecrypt($s_encryptedModelId);
		$o_model = $this->o_em->find('entities\IM_Model', $i_modelId);
		$o_modelStats = $this->o_em->getRepository('entities\IM_Model_stats')->findOneBy(array('id_mod'=>$i_modelId));
		$o_addModelForm = new ClaycodeForm('adminUpdateModel');
		
		//basic info
		$o_addModelForm->setFieldValue('model-name', $o_model->getName());
		$o_addModelForm->setFieldValue('model-full-name', $o_model->getFullName());
		if($o_model->getPublished() == 'x'){
			$b_published = true;
		}else{
			$b_published = false;
		}
		$o_addModelForm->setFieldValue('model-published', $b_published);
		$o_addModelForm->setFieldValue('model-height', $o_modelStats->getHeight());
		$o_addModelForm->setFieldValue('model-chest', $o_modelStats->getChest());
		$o_addModelForm->setFieldValue('model-waist', $o_modelStats->getWaist());
		$o_addModelForm->setFieldValue('model-hips', $o_modelStats->getHips());
		$o_addModelForm->setFieldValue('model-shoe', $o_modelStats->getShoe());
		$o_addModelForm->setFieldValue('model-hair', $o_modelStats->getHair());
		$o_addModelForm->setFieldValue('model-eyes', $o_modelStats->getEyes());
		$o_addModelForm->setFieldValue('add-model-submit', 'update model');
		$o_addModelForm->setFieldValue('model-ident', $s_encryptedModelId);
		
		
		//new fields
		if($o_model->getGender() == 'male'){
			$o_addModelForm->setFieldValue('model-gender-male', true);
		}else{
			$o_addModelForm->setFieldValue('model-gender-female', true);
		}
		
		if($o_model->getIsMainBoard() == 'x'){
			$o_addModelForm->setFieldValue('model-mainboard-yes', true);
		}else{
			$o_addModelForm->setFieldValue('model-mainboard-no', true);
		}
		
		if($o_model->getIsDevelopment() == 'x'){
			$o_addModelForm->setFieldValue('model-development-yes', true);
		}else{
			$o_addModelForm->setFieldValue('model-development-no', true);
		}
		
		
		if($o_model->getIsInTown() == 'x'){
			$o_addModelForm->setFieldValue('model-in-town-yes', true);
		}else{
			$o_addModelForm->setFieldValue('model-in-town-no', true);
		}
		
		$this->registry->template->s_mainAppDisplay = $o_addModelForm->getFormHtml();
		$this->getSubNavi();
		$this->registry->template->show('Admin-ManageModels.php');
		
	}	
	
	public function deleteModel(){
		$i_modelId = $_GET['ident'];
		$i_modelId = EncryptionHandler::get()->doDecrypt($i_modelId);
		$o_model = $this->o_em->find('entities\IM_Model', $i_modelId);
		$o_modelStats = $this->o_em->getRepository('entities\IM_Model_stats')->findOneBy(array('id_mod'=>$i_modelId));
		$arr_o_modelPictures = $this->o_em->getRepository('entities\IM_Model_picture')->getModelPictureByModelId($i_modelId);
		$this->o_em->remove($o_model);
		$this->o_em->remove($o_modelStats);
		
		//if not empty
		//loop through the pics
		if(!empty($arr_o_modelPictures)){
			foreach($arr_o_modelPictures as $o_pic){
				$s_picsPath = C_FW_ABSPATH.'/im_images_mod/pictures'. '/' . $o_pic->getRelPath();
				$s_thumbsPath = C_FW_ABSPATH.'/im_images_mod/thumbnails'. '/' . $o_pic->getRelPath();
				if(is_dir($s_picsPath) && is_dir($s_thumbsPath)){
					if(file_exists($s_picsPath . '/'. $o_pic->getFileName()) && file_exists($s_thumbsPath .'/'. $o_pic->getFileName())){
						unlink($s_picsPath . '/'. $o_pic->getFileName());
						unlink($s_thumbsPath .'/'. $o_pic->getFileName());
						$this->o_em->remove($o_pic);
					}
				}
				
			}
			if(is_dir($s_picsPath) && is_dir($s_thumbsPath)){
				rmdir($s_picsPath);
				rmdir($s_thumbsPath);
			}
		}
				
		$this->o_em->flush();
		fw_util_redirect('AdminShowModels');
	}
	
	private function getModelsCategoryFilter(){
		
		$s_modelCatFilter = '<div id="model-cat-filter" style="margin:20px 20px 20px 20px;">';
		$s_modelCatFilter .= '<ul>';
		$s_modelCatFilter .= '<li><input type="radio" value="all" checked="true" name="model-category" id="model-category">all</li>';
		$s_modelCatFilter .= '<li><input type="radio" value="mainboard" name="model-category" id="model-category">main board</li>';
		$s_modelCatFilter .= '<li><input type="radio" value="intown" name="model-category" id="model-category">in town</li>';
		$s_modelCatFilter .= '<li><input type="radio" value="development" name="model-category" id="model-category">development</li>';
		$s_modelCatFilter .= '</ul>';
		$s_modelCatFilter .= '</div>';
		
		return $s_modelCatFilter;
	}
	
	private function getModelsTable()
	{
		//get the html
		//render it to display
		
		
		$s_viewModelsTableHtml = $this->getModelsCategoryFilter();
		$s_viewModelsTableHtml .= '<table id="models-table">';
		
		$s_viewModelsTableHtml .= '<thead>';
		$s_viewModelsTableHtml .= '<tr>';
		$s_viewModelsTableHtml .= '<th>name</th>';
		$s_viewModelsTableHtml .= '<th>full name</th>';
		$s_viewModelsTableHtml .= '<th>gender</th>';
		$s_viewModelsTableHtml .= '<th>date updated</th>';
		$s_viewModelsTableHtml .= '<th>published</th>';
		//actions
		$s_viewModelsTableHtml .= '<th>manage pictures</th>';
		$s_viewModelsTableHtml .= '<th>update info</th>';
		$s_viewModelsTableHtml .= '<th>delete</th>';
		//end actions
		$s_viewModelsTableHtml .= '</tr>';
		$s_viewModelsTableHtml .= '</thead>';
		$s_viewModelsTableHtml .= '<tbody>';
		$s_viewModelsTableHtml .= '<tr><td colspan="5" class="dataTables_empty">Loading data from server</td></tr>';
		
		$s_viewModelsTableHtml .= '</tbody>';
		
		$s_viewModelsTableHtml .= '</table>';
		
		return $s_viewModelsTableHtml;
	}
	
	public function doModelsTableAction()
	{
		if($_SERVER['REQUEST_METHOD'] == 'GET'){
			$arr_request = $_GET;
		}else{
			$arr_request = $_POST;
		}
		
		//pagination
		if(isset($arr_request['iDisplayStart']) && isset($arr_request['iDisplayLength'])){
			//offset of displayed records
			$this->setOffset($arr_request['iDisplayStart']);
	
			//max amount of records to display
			$this->setMaxShown($arr_request['iDisplayLength']);
				
			//order by, sorting
			$this->arr_currPageObjects = $this->o_em->getRepository('entities\IM_Model')->getModelsByPage(
				$this->getOffset(),
				$this->getMaxShown(),
				$arr_request['iSortCol_0'],
				$arr_request['sSortDir_0'],
				intval($arr_request['sEcho']),
				$arr_request['sSearch'],
				$arr_request['_c_cat']
			);
			fw_util_outputJson($this->arr_currPageObjects);
		}
	}
	
	private function setOffset($i_offset = 0)
	{
		if(trim($i_offset) != ''){
			if(is_numeric($i_offset)){
				$this->i_offset = (int)htmlspecialchars($i_offset);
			}
		}
	}
	
	private function setMaxShown($i_maxShown = 10)
	{
		if(trim($i_maxShown) != ''){
			if(is_numeric($i_maxShown)){
				$this->i_maxShown = (int)htmlspecialchars($i_maxShown);
			}
		}
	}
	
	private function getOffset()
	{
		return $this->i_offset;
	}
	
	private function getMaxShown()
	{
		return $this->i_maxShown;
	}
	
	public function handleAddModelForm()
	{
		$o_addModelForm = new ClaycodeForm('adminAddModel');
		$o_addModelForm->validateForm();
		if(count($o_addModelForm->getFormErrors()) == 0){
			$arr_inputVals = $o_addModelForm->getInputValues();
			//model entity\
			$o_newModel = new entities\IM_Model();
			//model stats entity
			$o_newModelStats = new entities\IM_Model_stats();
			
			/*
			//test case
			
			for($i=9;$i< 20;$i++){
				$o_newModel = new entities\IM_Model();
				//model stats entity
				$o_newModelStats = new entities\IM_Model_stats();						
				$o_newModel->setName('model name '.$i);
				$o_newModel->setFullname('model full name '.$i);
				$o_newModel->setPublished(true);
				$o_newModel->setCoverPhotoId('');
				//new fiels
				$o_newModel->setGender('female');
				$o_newModel->setIsMainBoard('y');
				$o_newModel->setIsDevelopment('y');
				$o_newModel->setIsInTown('y');
				$o_newModel->setUpdateDate(new \DateTime("now"));
				//persist the new model
				$this->o_em->persist($o_newModel);
				$this->o_em->flush();
				//vital statistics
				$o_newModelStats->setModelId($o_newModel->getId());
				$o_newModelStats->setHeight('5\'7"');
				$o_newModelStats->setChest('34');
				$o_newModelStats->setWaist('24');
				$o_newModelStats->setHips('34');
				$o_newModelStats->setShoe(8);
				$o_newModelStats->setHair('scarlet');
				$o_newModelStats->setEyes('brown');
				$o_newModelStats->setUpdateDate(new \DateTime("now"));
				//persist the new model stats
				$this->o_em->persist($o_newModelStats);
				$this->o_em->flush();
			
			}
			*/
			//begin the creation of the new entity
			//basic model info
			$o_newModel->setName($arr_inputVals['model-name']);
			$o_newModel->setFullname($arr_inputVals['model-full-name']);
			$o_newModel->setPublished($arr_inputVals['model-published']);
			$o_newModel->setCoverPhotoId('');
			//new fiels
			$o_newModel->setGender($arr_inputVals['model-gender']);
			$o_newModel->setIsMainBoard($arr_inputVals['model-mainboard']);
			$o_newModel->setIsDevelopment($arr_inputVals['model-development']);
			$o_newModel->setIsInTown($arr_inputVals['model-in-town']);
			$o_newModel->setUpdateDate(new \DateTime("now"));
			//persist the new model
			$this->o_em->persist($o_newModel);
			$this->o_em->flush();
			//vital statistics
			$o_newModelStats->setModelId($o_newModel->getId());
			$o_newModelStats->setHeight($arr_inputVals['model-height']);
			$o_newModelStats->setChest($arr_inputVals['model-chest']);
			$o_newModelStats->setWaist($arr_inputVals['model-waist']);
			$o_newModelStats->setHips($arr_inputVals['model-hips']);
			$o_newModelStats->setShoe($arr_inputVals['model-shoe']);
			$o_newModelStats->setHair($arr_inputVals['model-hair']);
			$o_newModelStats->setEyes($arr_inputVals['model-eyes']);
			$o_newModelStats->setUpdateDate(new \DateTime("now"));
			//persist the new model stats
			$this->o_em->persist($o_newModelStats);
			$this->o_em->flush();
			
			fw_util_redirect('AdminShowModels');
		}else{
			$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
			$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
			
			$this->registry->template->s_mainAppDisplay = $o_addModelForm->getFormHtml();
			$this->getSubNavi();
			$this->registry->template->show('Admin-ManageModels.php');
		}
	}
	
	public function handleUpdateModelForm(){
		$o_addModelForm = new ClaycodeForm('adminUpdateModel');
		$o_addModelForm->validateForm();
		if(count($o_addModelForm->getFormErrors()) == 0){
			$arr_inputVals = $o_addModelForm->getInputValues();
			//model entity
			$i_modelId = EncryptionHandler::get()->doDecrypt($arr_inputVals['model-ident']);
			$o_newModel = $this->o_em->find('entities\IM_Model', $i_modelId);
			$o_newModelStats = $this->o_em->getRepository('entities\IM_Model_stats')->findOneBy(array('id_mod'=>$i_modelId));
			
			//begin the creation of the new entity
			//basic model info
			$o_newModel->setName($arr_inputVals['model-name']);
			$o_newModel->setFullname($arr_inputVals['model-full-name']);
			$o_newModel->setPublished($arr_inputVals['model-published']);
			$o_newModel->setCoverPhotoId('');
			//new fields
			$o_newModel->setGender($arr_inputVals['model-gender']);
			$o_newModel->setIsMainBoard($arr_inputVals['model-mainboard']);
			$o_newModel->setIsDevelopment($arr_inputVals['model-development']);
			$o_newModel->setIsInTown($arr_inputVals['model-in-town']);
			
			$o_newModel->setUpdateDate(new \DateTime("now"));
			//persist the new model
			$this->o_em->merge($o_newModel);
			$this->o_em->flush();
			//vital statistics
			
			
			$o_newModelStats->setModelId($i_modelId);
			$o_newModelStats->setHeight($arr_inputVals['model-height']);
			$o_newModelStats->setChest($arr_inputVals['model-chest']);
			$o_newModelStats->setWaist($arr_inputVals['model-waist']);
			$o_newModelStats->setHips($arr_inputVals['model-hips']);
			$o_newModelStats->setShoe($arr_inputVals['model-shoe']);
			$o_newModelStats->setHair($arr_inputVals['model-hair']);
			$o_newModelStats->setEyes($arr_inputVals['model-eyes']);
			$o_newModelStats->setUpdateDate(new \DateTime("now"));
			
			//persist the new model stats
			$this->o_em->merge($o_newModelStats);
			$this->o_em->flush();
			
			fw_util_redirect('AdminShowModels');
		}else{
			$this->registry->template->arr_jsScriptUrls = $this->arr_appConf['js_files'];
			$this->registry->template->arr_cssSriptUrls = $this->arr_appConf['css_style_sheets'];
			
			$this->registry->template->s_mainAppDisplay = $o_addModelForm->getFormHtml();
			$this->getSubNavi();
			$this->registry->template->show('Admin-ManageModels.php');
		}
	}
	
}